<?php

namespace App\Controller\Admin\Common;

use App\Controller\Admin\BaseController;
use App\Service\Common\SettingService;
use App\Validator\Admin\Common\SettingValidator;
use Hyperf\Di\Annotation\Inject;

class SettingController extends BaseController
{

    #[Inject]
    protected SettingValidator $settingValidator;

    #[Inject]
    protected SettingService $settingService;

    public function pageList()
    {
        $params = $this->allParams();
        [$va, $page, $ps] = $this->settingValidator->pageList($params);
        $lists = $this->settingService->pageListForAdmin($va, $page, $ps);
        return $this->success($lists);
    }

    public function detail()
    {
    }

    public function add()
    {
        $params = $this->allParams();
        $va = $this->settingValidator->add($params);
        [$r, $msg] = $this->settingService->addOne($va);
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function update()
    {
        $params = $this->allParams();
        $va = $this->settingValidator->update($params);
        [$r, $msg] = $this->settingService->updateSetting(intval($va['id']), $va);
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function del()
    {
        $params = $this->allParams();
        $va = $this->settingValidator->del($params);
        [$r, $msg] = $this->settingService->delSettingByIds($va['ids']);
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function refreshAll()
    {
        [$r, $msg] = $this->settingService->refreshAll();
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }
}